<?php
/**
 * 优惠券管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Coupon;

use App\Admin\Actions\Coupon\BatchOffShelf;
use App\Admin\Actions\Coupon\BatchOnShelf;
use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\CouponModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\GoodsWarehouseModel;
use App\Models\yb\UserCouponModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;


class CouponController extends Controller
{
    use ModelForm;

    protected $header = '优惠券管理';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    /**
     * 详情
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->detailGrid($id));

        });
    }

    public function grid(){

        $grid = new Grid(new CouponModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('id','编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('name','优惠券名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('promotion_full_reduce','满减')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('received_number','已领取')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('validity_after','领取之后有效期')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('used_number','已使用')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $grid->column('distribution_type','优惠券类型')->using(DataDictionaryModel::DistributionType, '未知')->label([
            0 => 'danger',
            1 => 'success',
            2=> 'info',
            3=> 'warning'
        ], 'info');

        $grid->column('status','状态')->using([0=>'禁用',1=>'启用','2'=>'未开始',3=>'已过期'], '未知')->label([
            0 => 'danger',
            1 => 'success',
            2=> 'info',
            3=> 'warning'
        ], 'success');

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('name','优惠券名称');
                $filter->between('created_at', '创建时间')->datetime();
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('status', '状态')->select(DataDictionaryModel::Status);
            });

            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->actions(function($actions){
            $actions->disableView();
        });

        $grid->actions(function($actions){
            $actions->disableDelete();
            if( $actions->row['status'] == 1){
                $actions->disableEdit();
            }
        });

        $grid->tools(function ($tools) {
            $tools->append(new BatchOnShelf());
            $tools->append(new BatchOffShelf());
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new CouponModel());

        $form->text('name', '优惠券标题')->rules('required|max:150');

        $form->radio('type', '品类')
            ->options(DataDictionaryModel::CouponType)->when(2, function (Form $form) {
                $form->select('goods_type', '商品类型')->options(GoodsTypeModel::selectOptions())->rules('required');
            })->when(3, function (Form $form) {
                $form->select('goods_warehouse_id', '商品')->options(GoodsWarehouseModel::selectOptions())->rules('required');
            })->default(1);

        $form->html(view('admin.coupon.promotion'),'促销');

//        $form->dateRange('validity_start', 'validity_end', '有效期');

        $form->number('validity_after', '领取之后有效期')->rules('required');

        $form->radio('distribution_type', '优惠券类型')->options(DataDictionaryModel::DistributionType)->rules('required')->help('选择普通优惠券时会直接展示在待领取列表');

        $form->currency('required_points','积分')->symbol('积分')->help('如果是积分兑换此值必填');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->saved(function (Form $form) {
            $id = $form->model()->id;
            CouponModel::where(['id'=>$id])->update([
                'promotion_full'=>$form->promotion_full,
                'promotion_reduce'=>$form->promotion_reduce,

            ]);

        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        return $form;
    }

    public function detailGrid($id){

        $grid = new Grid(new UserCouponModel());

        $grid->model()->where(['coupon_id'=>$id])->orderBy('created_at', 'DESC');

        $grid->column('id','编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('coupon_name','优惠券名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.nickname','用户昵称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.phone','用户电话')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('status','状态')->using(['0'=>'未使用','1'=>'已使用'], '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');


        $grid->column('usage_time','使用时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('created_at','领取时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('User.nickname','用户昵称');
                $filter->like('User.phone','用户电话');
                $filter->equal('status', '状态')->select(['0'=>'未使用','1'=>'已使用']);

            });
            $filter->column(1/2, function ($filter) {
                $filter->between('usage_time', '使用时间')->datetime();
                $filter->between('created_at', '领取时间')->datetime();
            });

            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->actions(function($actions){
            $actions->disableView();
        });

        $grid->disableActions();

        $grid->disableCreateButton();

        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;
    }

}
